package com.example.android.gorillas.kanji2roma;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.net.Uri;

public class Kanji2RomaByWeb {
	private static final String API_KEY = "IAyo.OCxg66TOKi2Vt6jRpkN5ACMGIHRN13KUe4yUwXkVkPhXoPO3A5ZQMCs6kw-";
	private static final String API_URL = "http://jlp.yahooapis.jp/FuriganaService/V1/furigana";
	
	public void prepareConnection() {
		// create HTTP client
	}
	
	private String buildRequestUrl(String inputString) {
		StringBuilder uriBuilder = new StringBuilder();
		
		uriBuilder.append(API_URL);
		uriBuilder.append("?");
		uriBuilder.append("appid=");
		uriBuilder.append(API_KEY);
		uriBuilder.append("&sentence=");
		uriBuilder.append(Uri.encode(inputString));
		
		return uriBuilder.toString();
	}
	
	public String getRoman(String inputString) {
		HttpClient httpClient = new DefaultHttpClient();
		String uriString = buildRequestUrl(inputString);
		HttpGet request = new HttpGet(uriString);
		HttpResponse response;
		
		try {
			response = httpClient.execute(request);
		} catch (ClientProtocolException e) {
			return null;
		} catch (IOException e) {
			return null;
		}
		
		int status = response.getStatusLine().getStatusCode();
		
		if (status != HttpStatus.SC_OK)
			return null;
		
		try {
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			response.getEntity().writeTo(outStream);
			final XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			final XmlPullParser parser = factory.newPullParser();
			
			String xmlString = new String(outStream.toByteArray(), "UTF-8");
			parser.setInput(new StringReader(xmlString));
			StringBuilder romanBuilder = new StringBuilder();
			
			int eventType = parser.getEventType();
			
			int tag = 0;
			String tagName = null;
			String surfaceText = null;
			String romanText = null;
			
			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG) {
					tagName = parser.getName();
					
					if (tagName.equals("Surface")) {
						eventType = parser.next();
						tag = 1;
					} else if (tagName.equals("Roman")) {
						eventType = parser.next();
						tag = 2;
					} else {
						eventType = parser.next();
						continue;
					}

					if (eventType == XmlPullParser.TEXT) {
						if (tag == 1) {
							surfaceText = parser.getText();
						} else if (tag == 2) {
							romanText = parser.getText();
						}
						eventType = parser.next();
					}
				} else if (eventType == XmlPullParser.END_TAG) {
					tagName = parser.getName();
					
					if (tagName.equals("Word")) {
						if (romanText != null)
							romanBuilder.append(romanText);
						else if (surfaceText != null)
							romanBuilder.append(surfaceText);
						
						romanText = null;
						surfaceText = null;
					}
				}
				
				eventType = parser.next();
			}

			return romanBuilder.toString();
		} catch (IOException e) {
			return null;
		} catch (XmlPullParserException e) {
			return null;
		}
	}
}
